﻿
namespace TransportAnalizer.DataTests
{
    using System;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using TransportAnalizer_2._0.Model;
    [TestClass]
    public class NodeBusinessContextTests:FunctionalTest
    {
        #region NodeTests

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void AddNewNode_ThrowsException_WhenLocationIsNull()
        {
            using (var bc = new NodeBusinessContext())
            {
                var node = new Node
                {
                  Location=null,
                  NodeType = new NodeType { Name = "1", Regulation = RegulationType.Stop, Passage = 30, Description = "описание" }
                };

                bc.CreateNode(node);
            }
        }

       
        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void AddNewNode_ThrowsException_WhenNodeTypesIsEmpty()
        {
            using (var bc = new NodeBusinessContext())
            {
                var node = new Node
                {
                    Location = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-122.206834 57.611421)"),
                    NodeType = new NodeType { Name = "1", Regulation = RegulationType.Stop, Passage = 30, Description = "описание" }
                };

                bc.CreateNode(node);
            }
        }

        [TestMethod]
        public void AddNewNode_NodeIsStoredInDataStore()
        {
            using (var bc = new NodeBusinessContext())
            {
                var bcNodeType= new NodeTypeBusinessContext();
                bcNodeType.DataContext=bc.DataContext;

                NodeType nodeType = new NodeType { Name = "1", Regulation = RegulationType.Stop, Passage = 30, Description = "описание" };
                bcNodeType.CreateNodeType(nodeType);

                var node = new Node
                {
                    Location = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-122.206834 57.611421)"),
                };
                bc.CreateNode(node);
                bool exists = bc.DataContext.NodeSet.Any(c => c.Id == node.Id);
                Assert.IsTrue(exists);
            }
        }

        [TestMethod]
        public void UpdateNode_AppliedValuesAreStoredInDataStore()
        {
            using (var bc = new NodeBusinessContext())
            {
                var bcNodeType = new NodeTypeBusinessContext();
                bcNodeType.DataContext = bc.DataContext;
                NodeType nodeType = new NodeType { Name = "1", Regulation = RegulationType.Stop, Passage = 30, Description = "описание" };
                bcNodeType.CreateNodeType(nodeType);
               
                // Arrange
                var node = new Node
                {
                    Location = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-122.206834 57.611421)"),
                };

                bc.CreateNode(node);

                System.Data.Entity.Spatial.DbGeography newLocation = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-123.206834 58.611421)");
                NodeType newNodeType = new NodeType { Name = "2", Regulation = RegulationType.Stop, Passage = 30, Description = "описание" };
                bcNodeType.CreateNodeType(newNodeType);

                node.Location = newLocation;
                node.NodeType = newNodeType;

                // Act
                bc.UpdateNode(node);

                // Assert
                bc.DataContext.Entry(node).Reload();

                Assert.AreEqual(newLocation.Latitude, node.Location.Latitude);
                Assert.AreEqual(newLocation.Longitude, node.Location.Longitude);
                Assert.AreSame(newNodeType, node.NodeType);
            }
        }

        [TestMethod]
        public void GetNodeList_ReturnsExpectedListOfNodeEntities()
        {
            using (var bc = new NodeBusinessContext())
            {
                var bcNodeType = new NodeTypeBusinessContext();
                bcNodeType.DataContext = bc.DataContext;
                NodeType nodeType = new NodeType { Name = "1", Regulation = RegulationType.Stop, Passage = 30, Description = "описание" };
                bcNodeType.CreateNodeType(nodeType);

                bc.CreateNode(new Node
                {
                    Location = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-122.206834 58.611421)")
                });
                bc.CreateNode(new Node
                {
                    Location = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-122.206834 57.611421)")
                });
                bc.CreateNode(new Node
                {
                    Location = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-122.206834 59.611421)")
                });

                var customers = bc.GetNodeList();

                Assert.IsTrue(customers.ElementAt(0).Id == 1);
                Assert.IsTrue(customers.ElementAt(1).Id == 2);
                Assert.IsTrue(customers.ElementAt(2).Id == 3);

                // Use CollectionAssert by overriding Equals in entities
                // or using IComparer in CollectionAssert for a more full-proof
                // solution
            }
        }

        [TestMethod]
        public void DeleteCustomer_RemovesCustomerFromDataStore()
        {
            using (var bc = new NodeBusinessContext())
            {
                var bcNodeType = new NodeTypeBusinessContext();
                bcNodeType.DataContext = bc.DataContext;
                NodeType nodeType = new NodeType { Name = "1", Regulation = RegulationType.Stop, Passage = 30, Description = "описание" };
                bcNodeType.CreateNodeType(nodeType);

                var node=new Node
                 {
                     Location = System.Data.Entity.Spatial.DbGeography.FromText("POINT(-122.206834 58.611421)"),
                     NodeType = nodeType
                 };
                bc.CreateNode(node);
                bc.DeleteNode(node);
                Assert.IsFalse(bc.DataContext.NodeSet.Any());
            }
        }

        #endregion NodeTests



    }
}
